﻿using System.Configuration;
using System.Windows.Forms;
using Citi.BOExtraction.Util;
using EO.WebBrowser;
using EO.WebBrowser.WinForm;

namespace Citi.FinancePlanningAnalysis.WindowsUI.BoExtraction
{
    public class Extract : BusinessObjectsExtract
    {
         public Extract(WebControl webControl, string reportName, string startUrl, string singleSignOnUrl, string userLogin, string pathToSave,
            string parameters, bool newWindowNoAction, Form form = null)
            : base(webControl, reportName, startUrl, singleSignOnUrl, userLogin, pathToSave, parameters, newWindowNoAction, form)
        { }

        public override void InboxDocumentLoadCompleted()
        {
            WebBrowser.NewWindow -= webBrowser_NewWindowNoAction;
            WebBrowser.NewWindow += webBrowser_NewWindowOpenTargetUrl;

            Log("Looking for BO LINK");
            var linkReportingView = "frames['ptifrmtgtframe'].contentDocument.getElementById('APS_VIEW_BO_WRK_APS_BO_RPT_LINK')";
            WebBrowser.WaitUntilExist(linkReportingView, 90000);
            Log("Clicking on BO LINK");
            ClickOnElement(linkReportingView, null, null);

            Log("Waiting");
            Wait(90000);

            Log("Loading UrlFolderStart");
            WebBrowser.LoadUrlAndWait(ConfigurationManager.AppSettings["UrlFolderStart"]);

            base.InboxDocumentLoadCompleted();
        }

        void WebBrowser_LoadComplete(object sender, NavigationTaskEventArgs e)
        {
            WebBrowser.LoadUrl(ConfigurationManager.AppSettings["UrlFolderStart"]);

            base.InboxDocumentLoadCompleted();
        }
    }
}
